﻿using System;
using Medianamik.Core.Caching;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using SubSonic;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public class CompanyRepository<TCollection, TItem> : 
        MedianamikRepository<TCollection, TItem>, 
        ICompanyRepository<TItem>
        where TItem : ICompany, IRecordBase, new()
        where TCollection : ICollectionWithChildren<TCollection, TItem>, new()
    {
        public CompanyRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper) : base(cultureService, contentStateService, nodeMapper)
        {
        }
    }

    public class CompanyRepository : CompanyRepository<CompanyCollection, Company>, ICompanyRepository
    {
        private readonly ICacheService<Company> _cacheService;

        public CompanyRepository(ICultureService cultureService, IContentStateService contentStateService,
            INodeMapper nodeMapper, ICacheService<Company> cacheService)
            : base(cultureService, contentStateService, nodeMapper)
        {
            _cacheService = cacheService;
        }

        public override Company Get(Guid nodeId)
        {
            return _cacheService.Get(nodeId + "_" + CultureService.GetCurrentCulture(),
                                    Company.GetCacheDependency(),
                                     () => GetForCache(nodeId));
        }

        private Company GetForCache(Guid nodeId)
        {
            return base.Get(nodeId);
        }
    }

    public interface ICompanyRepository<T> : IMedianamikRepository<T>
        where T : ICompany
    {
    }

    public interface ICompanyRepository : ICompanyRepository<Company>
    {
    }
}
